package tt.dz.appserver.dao.base;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.map.ListOrderedMap;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.util.Assert;
import org.springframework.util.SerializationUtils;

import tt.dz.appserver.util.redis.TokenIdentify;

/**
 * redis操作类
 * @author 谢龙飞
 * <p>Date: 11-19
 *
 */
public class RedisCacheDao {

	/**LOG*/
	private final static Logger LOG  = Logger.getLogger("Redis 存储器======================>");
	
	/**redis template*/
	@Autowired
	private RedisTemplate<String, TokenIdentify> redisTemplate;

	/**  
     * 新增 
     *<br>------------------------------<br> 
     * @param key key值
     * @param obj value对象
     * @return 
     */  
    public boolean add(final String key,final Object obj) { 
    	
    	if(!(obj instanceof Serializable)){
    		throw new SerializationException("存入对象不是有效序列化对象");
    	}
    	else{
	        return redisTemplate.execute(new RedisCallback<Boolean>() {  
	            public Boolean doInRedis(RedisConnection connection)  
	                    throws DataAccessException {  
	                byte[] byteKey  = SerializationUtils.serialize(key);  
	                byte[] byteValue = SerializationUtils.serialize(obj); 
	                LOG.info("存入值:"+key);
	                return connection.setNX(byteKey, byteValue);  
	            }  
	        });  
    	}
    }  
      
    /** 
     * 批量新增 
     *<br>------------------------------<br> 
     *@param listOrderedMap 存入map集合 
     *@return 
     */  
    public boolean add(final ListOrderedMap listOrderedMap) {  
        Assert.notEmpty(listOrderedMap);
       
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {  
            public Boolean doInRedis(RedisConnection connection)  
                    throws DataAccessException {  
            	
            	for (int i = 0; i < listOrderedMap.size(); i++) {
            		String key = (String) listOrderedMap.get(i);
            		Object obj = listOrderedMap.getValue(i);
            		
            		if(!(obj instanceof Serializable)){
            			throw new SerializationException("存入对象不是有效序列化对象");
            	    }
            		else{
	            		byte[] byteKey  = SerializationUtils.serialize(key);  
	                    byte[] byteValue = SerializationUtils.serialize(obj);  
	                    connection.setNX(byteKey, byteValue);  
            		}
				}
                return true;  
            }  
        }, false, true);  
        return result;  
    }  
      
    /**  
     * 删除 
     * <br>------------------------------<br> 
     * @param key 
     */  
    public void delete(String key) {  
        List<String> list = new ArrayList<String>();  
        list.add(key);    
        LOG.info("删除值:"+key);
        delete(list);  
    }  
  
    /** 
     * 删除多个 
     * <br>------------------------------<br> 
     * @param keys 
     */  
    public void delete(List<String> keys) {  
        redisTemplate.delete(keys);  
    }  
  
    /** 
     * 修改  
     * <br>------------------------------<br> 
     * @param key key值
     * @param obj 存入对象 
     * @return  
     */  
    public boolean update(final String key,final Object obj) {  
    	
        if (get(key) == null) {  
            throw new NullPointerException("修改数据行不存在, key = " + key);
        }
        if(!(obj instanceof Serializable)){
			throw new SerializationException("存入对象不是有效序列化对象");
	    }
        return redisTemplate.execute(new RedisCallback<Boolean>() {  
            public Boolean doInRedis(RedisConnection connection)  
                    throws DataAccessException {  
                byte[] byteKey  = SerializationUtils.serialize(key);  
                byte[] byteValue = SerializationUtils.serialize(obj);  
                LOG.info("修改值:"+key);
                return connection.setNX(byteKey, byteValue);  
            }  
        });  
    }  
  
    /**  
     * 通过key获取 
     * <br>------------------------------<br> 
     * @param key key值
     * @return 
     */  
    public TokenIdentify get(final String key) {  

    	return redisTemplate.execute(new RedisCallback<TokenIdentify>() {
    		
            public TokenIdentify doInRedis(RedisConnection connection)  
                    throws DataAccessException {  
                byte[] byteKey  = SerializationUtils.serialize(key);  
                byte[] value = connection.get(byteKey);   
                LOG.info("获取值:"+key);
                if (value == null) {  
                    return null;  
                }  
                return (TokenIdentify) SerializationUtils.deserialize(value);  
            }  
        });  
    }  
}
